﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace QLKS
{
    public partial class QLCV : Form
    {
        public f_Login flogin = new f_Login();
        public Dictionary<string, string> GetPassFromUser;
        public Mysql qmysql = new Mysql();
        public QLCV()
        {
            InitializeComponent();
        }

        private void QLCV_Load(object sender, EventArgs e)
        {
            //Getdata from database.
            string cmdText = "SELECT * FROM ht_chucvu";
            Dictionary<string, string> Params = new Dictionary<string, string>();
            DataTable GetAllChucVu = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params);
            grv_CV.DataSource = GetAllChucVu;
        }

        private void grv_CV_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            tx_MaCV.Text = grv_CV.Rows[e.RowIndex].Cells["MaCV"].Value.ToString();
            tx_TenCV.Text = grv_CV.Rows[e.RowIndex].Cells["TenCV"].Value.ToString();
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            int error = 0;
            string tmp_tencv = Regex.Replace(tx_TenCV.Text.Trim(), @"\W\s+", "");
            if (tx_TenCV.Text!="")
            {
                //Get TenCV
                string cmdTextTenCV = "SELECT * FROM ht_chucvu WHERE TenCV=?tencv LIMIT 0,1";
                Dictionary<string, string> ParamsTenCV = new Dictionary<string, string>();
                ParamsTenCV["tencv"] = tmp_tencv;
                Dictionary<string, string> GetTenCV = qmysql.SelectReaderSqlCommand(cmdTextTenCV, ParamsTenCV);
                if (GetTenCV.Count > 0)
                {
                    error = 1;
                    MessageBox.Show("Đã tồn tại Tên Chức Vụ này trong hệ thống", "Thông Báo");
                }
            }
            else
            {
                error = 1;
                MessageBox.Show("Vui lòng không để trống Tên Chức Vụ", "Thông Báo");
            }

            //check error and insert data
            if (error == 0)
            {
                string cmdTextIscv = "INSERT INTO ht_chucvu(TenCV) VALUES(?tencv)";
                Dictionary<string, string> Params_iscv = new Dictionary<string, string>();
                Params_iscv["tencv"] = tmp_tencv;
                this.qmysql.NonQueryMysqlCommand(cmdTextIscv, Params_iscv);
                
                //reload data
                //Getdata from database.
                string cmdText = "SELECT * FROM ht_chucvu";
                Dictionary<string, string> Params = new Dictionary<string, string>();
                DataTable GetAllChucVu = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params);
                grv_CV.DataSource = GetAllChucVu;
                this.grv_CV.Refresh();
            }
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            int error = 0;
            string tmp_tencv = Regex.Replace(tx_TenCV.Text.Trim(), @"\W\s+", "");
            string tmp_macv = Regex.Replace(tx_MaCV.Text.Trim(), @"\W\s+", "");
            if (tx_TenCV.Text != "")
            {
                //Get TenCV
                string cmdTextTenCV = "SELECT * FROM ht_chucvu WHERE TenCV=?tencv LIMIT 0,1";
                Dictionary<string, string> ParamsTenCV = new Dictionary<string, string>();
                ParamsTenCV["tencv"] = tmp_tencv;
                Dictionary<string, string> GetTenCV = qmysql.SelectReaderSqlCommand(cmdTextTenCV, ParamsTenCV);
                if (GetTenCV.Count > 1 && GetTenCV["MaCV"]!=tmp_macv)
                {
                    error = 1;
                    MessageBox.Show("Đã tồn tại Tên Chức Vụ này trong hệ thống", "Thông Báo");
                }
            }
            else
            {
                error = 1;
                MessageBox.Show("Vui lòng không để trống Tên Chức Vụ", "Thông Báo");
            }

            //check error and update data
            if (error == 0)
            {
                string cmdTextUscv = "UPDATE ht_chucvu SET TenCV=?tencv WHERE MaCV=?macv";
                Dictionary<string, string> Params_uscv = new Dictionary<string, string>();
                Params_uscv["tencv"] = tmp_tencv;
                Params_uscv["macv"] = tmp_macv;
                this.qmysql.NonQueryMysqlCommand(cmdTextUscv, Params_uscv);

                //reload data
                //Getdata from database.
                string cmdText = "SELECT * FROM ht_chucvu";
                Dictionary<string, string> Params = new Dictionary<string, string>();
                DataTable GetAllChucVu = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params);
                grv_CV.DataSource = GetAllChucVu;
                this.grv_CV.Refresh();
            }
        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            int error = 0;
            string tmp_macv = Regex.Replace(tx_MaCV.Text.Trim(), @"\W\s+", "");
            if (tmp_macv == "")
            {
                error = 1;
                MessageBox.Show("Không nhận diện được Mã Chức Vụ", "Thông Báo");
            }

            //check error and update data
            if (error == 0)
            {
                string cmdTextUscv = "DELETE FROM ht_chucvu WHERE MaCV=?macv";
                Dictionary<string, string> Params_uscv = new Dictionary<string, string>();
                Params_uscv["macv"] = tmp_macv;
                this.qmysql.NonQueryMysqlCommand(cmdTextUscv, Params_uscv);

                //reload data
                //Getdata from database.
                string cmdText = "SELECT * FROM ht_chucvu";
                Dictionary<string, string> Params = new Dictionary<string, string>();
                DataTable GetAllChucVu = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params);
                grv_CV.DataSource = GetAllChucVu;
                this.grv_CV.Refresh();
            }
        }
    }
}
